package com.jiacl.service.impl;

import com.github.pagehelper.PageInfo;
import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.dictionary.py.Pinyin;
import com.jiacl.core.CommonConstants;
import com.jiacl.entity.JiaclSurname;
import com.jiacl.entity.core.Result;
import com.jiacl.form.SurnameForm;
import com.jiacl.mapper.JiaclSurnameMapper;
import com.jiacl.service.SurnameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by XiaoWei on 2017/9/2.
 */
@Service
public class SurnameServiceImpl implements SurnameService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private JiaclSurnameMapper jiaclSurnameMapper;
    @Override
    public Result selectJiaclSurname(Integer id) {
        if(id<0){
            return new Result();
        }
        Object redisCache = redisTemplate.opsForValue().get(CommonConstants.SURNAME_REDIS_KEY + id);
        if(redisCache!=null){
            return new Result(redisCache);
        }
        JiaclSurname jiaclSurname = jiaclSurnameMapper.selectByPrimaryKey(id);
        redisTemplate.opsForValue().set(CommonConstants.SURNAME_REDIS_KEY+id,jiaclSurname,1, TimeUnit.DAYS);
        return new Result(jiaclSurname);
    }

    @Override
    public Result selectSurnameAll(SurnameForm surnameForm) {
        Object redisCache = redisTemplate.opsForValue().get(CommonConstants.ALL_SURNAME_REDIS_KEY+surnameForm.getPageNum()+"-"+surnameForm.getPageSize());
        if (redisCache!=null){
            return new Result(redisCache);
        }
        List<JiaclSurname> jiaclSurnames = jiaclSurnameMapper.selectSurnameAll(surnameForm.enablePaging());
        PageInfo<JiaclSurname> pageInfo = new PageInfo<>(jiaclSurnames);
        redisTemplate.opsForValue().set(CommonConstants.ALL_SURNAME_REDIS_KEY+surnameForm.getPageNum()+"-"+surnameForm.getPageSize(),pageInfo,1,TimeUnit.DAYS);
        return new Result(pageInfo);
    }

    @Override
    public Result selectSurnameAll() {
        List<JiaclSurname> jiaclSurnames = jiaclSurnameMapper.selectSurnameAll();
        return new Result(jiaclSurnames);
    }

    @Override
    public Result update(JiaclSurname jiaclSurname) {
        jiaclSurnameMapper.updateByPrimaryKeySelective(jiaclSurname);
        return new Result();
    }

    @Override
    public Result selectSurnameByPhonetic(String phonetic) {
        if (StringUtils.isEmpty(phonetic)){
            return new Result();
        }
        List<JiaclSurname> jiaclSurnames = jiaclSurnameMapper.selectSurnameByPhonetic(phonetic);
        return new Result(jiaclSurnames);
    }

    @Override
    public Result selectSurnameBySimplified(String surname) {
        if (StringUtils.isEmpty(surname)){
            return new Result();
        }
        JiaclSurname jiaclSurname = jiaclSurnameMapper.selectSurnameBySimplified(surname);
        return new Result(jiaclSurname);
    }

    public static void main(String[] args) {
        List<Pinyin> p = HanLP.convertToPinyinList("肖");
        System.out.println(p.get(0));
    }

}
